package sar

import org.springframework.dao.DataIntegrityViolationException

class StudentAttendantController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		def searchObject=new TeachingSession(params)
		if(searchObject.time){
			def programCourse=ProgramCourse.findByProgramAndCourse(Program.get(params.program.id.toLong()),Course.get(params.course.id.toLong()))
			def so=TeachingSession.findByProgramCourseAndTime(programCourse,searchObject.time)
			def studentAttendantInstanceList= StudentAttendant.findAllByTeachingSession(so)
			return [studentAttendantInstanceList: studentAttendantInstanceList, studentAttendantInstanceTotal: studentAttendantInstanceList.size(), sobj:searchObject]
		}

		[studentAttendantInstanceList: new ArrayList<StudentAttendant>(), studentAttendantInstanceTotal: 0, sobj:searchObject]
	}

	def create() {
		[studentAttendantInstance: new StudentAttendant(params)]
	}

	def save() {
		def studentAttendantInstance = new StudentAttendant(params)
		if (!studentAttendantInstance.save(flush: true)) {
			render(view: "create", model: [studentAttendantInstance: studentAttendantInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'studentAttendant.label', default: 'StudentAttendant'),
			studentAttendantInstance.id
		])
		redirect(action: "show", id: studentAttendantInstance.id)
	}

	def show(Long id) {
		def studentAttendantInstance = StudentAttendant.get(id)
		if (!studentAttendantInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "list")
			return
		}

		[studentAttendantInstance: studentAttendantInstance]
	}

	def edit(Long id) {
		def studentAttendantInstance = StudentAttendant.get(id)
		if (!studentAttendantInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "list")
			return
		}

		[studentAttendantInstance: studentAttendantInstance]
	}

	def update(Long id, Long version) {
		def studentAttendantInstance = StudentAttendant.get(id)
		if (!studentAttendantInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "list")
			return
		}

		if (version != null) {
			if (studentAttendantInstance.version > version) {
				studentAttendantInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'studentAttendant.label', default: 'StudentAttendant')] as Object[],
						"Another user has updated this StudentAttendant while you were editing")
				render(view: "edit", model: [studentAttendantInstance: studentAttendantInstance])
				return
			}
		}

		studentAttendantInstance.properties = params

		if (!studentAttendantInstance.save(flush: true)) {
			render(view: "edit", model: [studentAttendantInstance: studentAttendantInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'studentAttendant.label', default: 'StudentAttendant'),
			studentAttendantInstance.id
		])
		redirect(action: "show", id: studentAttendantInstance.id)
	}

	def delete(Long id) {
		def studentAttendantInstance = StudentAttendant.get(id)
		if (!studentAttendantInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "list")
			return
		}

		try {
			studentAttendantInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'studentAttendant.label', default: 'StudentAttendant'),
				id
			])
			redirect(action: "show", id: id)
		}
	}
	def attendant(){
		for(String i in params.keySet()){
			if(i.isInteger()){
				def item=StudentAttendant.get(i.toLong())
				println params.get(i)
				item.attendant=new Boolean( params.get(i)[0].toString())
				item.save(failOnError:true)
			}
		}
		redirect(action:"list")
	}
}
